package com.zl.smartmall.library.listener;

import java.util.List;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;
import com.zl.smartmall.library.po.ActivitiesBrandIconInfo;
import com.zl.smartmall.library.po.ActivitiesCommoditiesInfo;
import com.zl.smartmall.library.po.ActivitiesInfoType;
import com.zl.smartmall.library.po.ActivitiesLatestNoticeInfo;
import com.zl.smartmall.library.po.ActivitiesThemeMarketInfo;
import com.zl.smartmall.library.po.AdvertInfo;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;

/**
 * 商城活动(首)页数据请求监听类
 * @author 陈希然
 *
 */
public abstract class ActivitiesListener extends BaseListener {

	private static final String TAG = ActivitiesListener.class.getSimpleName();
	
	public static String userpoint;//登录用户的可用积分
	public static String getMessage_url() {
		return message_url;
	}

	public static void setMessage_url(String message_url) {
		ActivitiesListener.message_url = message_url;
	}

	public String getUserpoint() {
		return userpoint;
	}

	public void setUserpoint(String userpoint) {
		this.userpoint = userpoint;
	}
	public static String message_url;//物业发送通知的网页链接地址
	@Override
	public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
		
			try {
				CrashHandler.d(TAG, response+"");
				int status = response.getInt(Constants.REQ_STATUS);
				String msg = response.getString(Constants.REQ_MSG);
				CrashHandler.d(TAG, "商城活动(首)页服务器status = " + status);
				CrashHandler.d(TAG, "商城活动(首)页服务器msg = " + msg);
				
				switch(status){
				case Constants.REQ_OK:
					JSONObject jsonObject = response.getJSONObject(Constants.REQ_RESULT);
					userpoint = jsonObject.getString("user_point");
					message_url = jsonObject.getString("message_url");
					onResponseSuccess(
							AdvertInfo.parseArray(jsonObject.getJSONArray("brandsad"), Constants.ADVERT_AFFILIATION_ACTIVITIES), 
							ActivitiesBrandIconInfo.parse(jsonObject.getJSONArray("activitiesIcon"), ActivitiesInfoType.TYPE_CLASSIFY), 
							ActivitiesThemeMarketInfo.parse(jsonObject.getJSONArray("themeActsList")), 
							ActivitiesBrandIconInfo.parse(jsonObject.getJSONArray("activitiesList"), ActivitiesInfoType.TYPE_RECOMMEND_MERCHANT), 
							ActivitiesCommoditiesInfo.parse(jsonObject.getJSONArray("hotProduct")), 
							ActivitiesLatestNoticeInfo.parse(jsonObject.getJSONArray("notices_list")),userpoint,jsonObject.toString(),message_url);
					break;
				
				case Constants.REQ_OFFLINE:
					onOffline();
					break;
					
				case Constants.REQ_NO_MORE_DATA:
					onNoMoreData();
					break;
					
				default:
					onResponseFailed();
				}
				
			} catch (JSONException e) {
				onFailure(statusCode, headers, e, new JSONObject());
			}
	}
	
	@Override
	public void onFailure(int statusCode, Header[] headers,
			Throwable throwable, JSONObject errorResponse) {
		// TODO Auto-generated method stub
		CrashHandler.e(throwable);
		onResponseFailed();
	}
	
	/**
	 * 活动(首)页数据加载成功
	 * @param advertInfos 广告
	 * @param classify 分类列表
	 * @param activitiesThemeMarketInfos 主题市场
	 * @param activitiesBrandIconInfos 品牌列表
	 * @param commoditiesInfos 热卖单品列表
	 */
	public abstract void onResponseSuccess(List<AdvertInfo> advertInfos, List<ActivitiesBrandIconInfo> classify, 
			List<ActivitiesThemeMarketInfo> activitiesThemeMarketInfos, List<ActivitiesBrandIconInfo> activitiesBrandIconInfos,
			List<ActivitiesCommoditiesInfo> activitiesCommoditiesInfos, List<ActivitiesLatestNoticeInfo> activitiesLatestNoticeInfos,String userpoint,String json,String messageurl);
	
}
